/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GameRollCallPanel.java
 *
 * Created on 2009-9-27, 16:19:30
 */
package com.res0w.SMMS.ui.admin;

import com.res0w.SMMS.factory.DataBaseOperate;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;

/**
 *
 * @author res0w
 * @version 0.1
 * @since 2009-10-6
 * 
 */
public class GameRollCallPanel extends javax.swing.JPanel {

    /** Creates new form GameRollCallPanel */
    public GameRollCallPanel() {
        initComponents();
        canRollCallList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        getListTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        nameListTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setFont(new java.awt.Font("微软雅黑", 0, 12));
        setPreferredSize(new java.awt.Dimension(950, 530));

        jLabel1.setFont(new java.awt.Font("微软雅黑", 0, 18));
        jLabel1.setText("可点录项目：");

        getListTable.setFont(new java.awt.Font("微软雅黑", 0, 12));
        getListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "项目次序", "项目名称", "时间", "地点"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        getListTable.setColumnSelectionAllowed(false);
        getListTable.setRowHeight(40);
        getListTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                getListTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(getListTable);
        getListTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jLabel2.setFont(new java.awt.Font("微软雅黑", 0, 18));
        jLabel2.setText("运动员名单：");

        nameListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "运动员号", "学生号", "姓名", "检录项目", "检录状态"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        nameListTable.setColumnSelectionAllowed(true);
        jScrollPane2.setViewportView(nameListTable);
        nameListTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        nameListTable.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(new JCheckBox()));

        jButton1.setText("提交");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 399, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton1)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(69, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addGap(42, 42, 42))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void getListTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_getListTableMouseClicked
        // TODO add your handling code here:
        //ISSUE 1:在linux中对事件的响应和windows中不同
        if (evt.getButton() == MouseEvent.BUTTON1) {// 单击鼠标左键
            if (evt.getClickCount() == 2) {
                String items = getListTable.getModel().getValueAt(getListTable.getSelectedRow(), 1).toString().trim();
                nameList(items);
            }
        }
    }//GEN-LAST:event_getListTableMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        submit();
    }//GEN-LAST:event_jButton1ActionPerformed

//Create By Author
    private void canRollCallList() {
        try {
            int rowNum = 0;
            String sql = "select * from setting_schedule " +
                    "where isFinished=0 order by itemsPriority ASC";
            ResultSet rs = null;
            DataBaseOperate db = new DataBaseOperate();
            rs = db.dbQueryMuti(sql);
            while (rs.next()) {
                getListTable.setValueAt(rs.getString(1), rowNum, 0);
                getListTable.setValueAt(rs.getString(2), rowNum, 1);
                getListTable.setValueAt(rs.getString(3), rowNum, 2);
                getListTable.setValueAt(rs.getString(4), rowNum, 3);
                rowNum++;
            }
            db.dbClosed();
        } catch (SQLException ex) {
            Logger.getLogger(GameRollCallPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void submit(){
        int i=0;
        DataBaseOperate db=new DataBaseOperate();
        ArrayList sqlList=new ArrayList();
        while(nameListTable.getValueAt(i, 0)!=""){
            if(nameListTable.getValueAt(i,4).toString().trim()=="true"){
                sqlList.add("update signup_cachelist set rollCallSta=1 " +
                        "where athleteId='"+nameListTable.getValueAt(i,0).toString().trim()+"'" +
                        " and joinItems='"+nameListTable.getValueAt(i,3).toString().trim()+"'");
            }
            i++;
        }
        db.dbUpdateBatch(sqlList);
    }

    private void nameList(String items) {
        try {
            for (int i = 0; i < nameListTable.getModel().getRowCount(); i++) {
                for (int m = 0; m < nameListTable.getModel().getColumnCount(); m++) {
                    nameListTable.setValueAt("", i, m);
                }
            }
            int rowNum = 0;
            String sql = "select athleteId,sID,sName,rollCallSta,joinItems" +
                    " from signup_cachelist " +
                    "where rollCallSta!=1 and joinItems='" + items + "' order by sID ASC";
            ResultSet rs = null;
            DataBaseOperate db = new DataBaseOperate();
            rs = db.dbQueryMuti(sql);
            while (rs.next()) {
                nameListTable.setValueAt(rs.getString(1), rowNum, 0);
                nameListTable.setValueAt(rs.getString(2), rowNum, 1);
                nameListTable.setValueAt(rs.getString(3), rowNum, 2);
                nameListTable.setValueAt(rs.getString(5), rowNum, 3);
                rowNum++;
            }
            db.dbClosed();
        } catch (SQLException ex) {
            Logger.getLogger(GameRollCallPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable getListTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable nameListTable;
    // End of variables declaration//GEN-END:variables
}
